Skip to content

MappingModel

Base classes

Name Children Inherits
ListMixin
prettyqt.itemmodels.listmixin
Mixin for flat models which are backed by a python list (`self.items`).
AbstractTableModel
prettyqt.core.abstracttablemodel
Abstract model that can be subclassed to create table models.

⋔ Inheritance diagram

graph TD
  2138817516["itemmodels.MappingModel"]
  2612381175["itemmodels.ListMixin"]
  2751680898["builtins.object"]
  656210031["core.AbstractTableModel"]
  4092674192["core.AbstractTableModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  232985924["QtCore.QAbstractTableModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  2612381175 --> 2138817516
  2751680898 --> 2612381175
  656210031 --> 2138817516
  4092674192 --> 656210031
  3882849576 --> 4092674192
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  2751680898 --> 3882849576
  2751680898 --> 4092674192
  232985924 --> 656210031
  1898060022 --> 232985924
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: ListMixin, AbstractTableModel

Table model for a list of mappings. Columns are the keys of the first mapping.

Source code in prettyqt/itemmodels/mappingmodel.py
class MappingModel(itemmodels.ListMixin, core.AbstractTableModel):
    """Table model for a list of mappings. Columns are the keys of the first mapping."""

    SUPPORTS = Sequence[Mapping]

    @classmethod
    def supports(cls, instance) -> bool:
        match instance:
            case (Mapping(), *_):
                return True
            case _:
                return False

    def _keys(self) -> list:
        return list(self.items[0].keys()) if self.items else []

    def columnCount(self, parent: core.ModelIndex | None = None) -> int:
        parent = parent or core.ModelIndex()
        return 0 if parent.isValid() else len(self._keys())

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ) -> str | None:
        match orientation, role:
            case constants.HORIZONTAL, constants.DISPLAY_ROLE:
                keys = self._keys()
                return str(keys[section]) if 0 <= section < len(keys) else None
        return None

    def data(
        self,
        index: core.ModelIndex,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if not index.isValid():
            return None
        item = self.data_by_index(index)
        key = self._keys()[index.column()]
        value = item.get(key)
        match role:
            case constants.DISPLAY_ROLE if not isinstance(value, bool):
                return "" if value is None else str(value)
            case constants.CHECKSTATE_ROLE if isinstance(value, bool):
                return self.to_checkstate(value)
            case constants.EDIT_ROLE | constants.USER_ROLE:
                return value
        return None

:fa-brands-github: Show source on GitHub